package cn.exrick.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.exrick.mapper.TbFriendRequestMapper;
import cn.exrick.pojo.TbFriendRequest;
import cn.exrick.service.FriendRequestService;
import cn.exrick.utils.mybatis.Criteria;
import cn.exrick.utils.mybatis.Page;
import cn.exrick.utils.mybatis.PageParam;

@Service
public class FriendRequestServiceImpl extends BaseServiceImpl<TbFriendRequest, TbFriendRequestMapper> implements FriendRequestService {
    private final static Logger log = LoggerFactory.getLogger(FriendRequestServiceImpl.class);

    @Override
    public Logger getLogger() {
        return log;
    }

    public Criteria createCriteria(Map<String, Object> params) {
        Criteria criteria = createCriteria();
        if (params.get("friendId") != null) {
            criteria.andFieldEqualTo("friend_id", String.valueOf((Long) params.get("friendId")));
        }
        if (params.get("memberId") != null) {
            criteria.andFieldEqualTo("member_id", String.valueOf((Long) params.get("memberId")));
        }
        if (params.get("status") != null) {
            criteria.andFieldEqualTo("status", String.valueOf((Integer) params.get("status")));
        }
        return criteria;
    }

    @Autowired
    TbFriendRequestMapper tbFriendRequestMapper;

    @Override
    public TbFriendRequestMapper getMapper() {
        return tbFriendRequestMapper;
    }

    public List<TbFriendRequest> findByFriendAndMember(long friendId, long memberId, Integer status) {
        List<TbFriendRequest> list = findListByCriteria(new HashMap<String, Object>() {{
            this.put("friendId", friendId);
            this.put("memberId", memberId);
            if (status != null) {
                this.put("status", status);
            }
        }});
        return list;
    }

    public List<TbFriendRequest> findByMemberId(long memberId) {
        return findListByCriteria(new HashMap<String, Object>() {{
            this.put("memberId", memberId);
        }});
    }

    public Page<TbFriendRequest> findByMemberId(long memberId, PageParam pageParam) throws Exception {
        return findPageByCriteria(new HashMap<String, Object>() {{
            this.put("memberId", memberId);
        }}, pageParam);
    }
}
